package com.hw.one.dam2.service;

import com.alibaba.fastjson.JSONObject;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.DataTypeEnum;
import com.hw.one.dam2.dao.AirDao;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.entity.AIREntity;
import com.hw.one.dam2.param.AirParam;
import com.hw.one.dam2.po.MonitorBaseDataPo;
import com.hw.one.dam2.po.MonitorDataPo;
import com.hw.one.dam2.po.RegionAirQualityPO;
import com.hw.one.dam2.po.SiteAirQualityPO;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.dam2.util.WeatherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部
 * Created by 毛超然 on 2017/6/22 0009.
 */

@Service(value = "airService")
public class AirService implements IAirService {

    @Autowired
    private AirDao airDao;
    @Autowired
    private IRegionSiteDao regionSiteDao;

    /**
     * 根据区域编码、时间和表名得到空气质量数据
     *
     * @param paraMap
     * @return
     * @throws Exception
     */

    @Override
    public List<AIREntity> findAirDataByRegionCodesAndTime(Map<String, Object> paraMap) throws Exception {
        // 选中的区域编码
        String checkedRegionCodes = paraMap.get("checkedRegionCodes").toString();
        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // 表名
        String tableName = paraMap.get("tableName").toString();
        // 选择的区域编码字符串转换为数组
        String[] checkedRegionCodeArr = checkedRegionCodes.split(",");
        // 选中区域编码数组转换为列表
        List<String> checkedRegionCodeList = Arrays.asList(checkedRegionCodeArr);

        AirParam airParam = new AirParam();
        airParam.setStartTime(startTime);
        airParam.setEndTime(endTime);
        airParam.setRegionCodes(checkedRegionCodeList);
        airParam.setTable(tableName);

        List<RegionAirQualityPO> airDaiaMapList = airDao.findAirDataByRegionCodesAndTime(airParam);
        List<AIREntity> airNewList = new ArrayList<AIREntity>();
        AIREntity airEntity = null;
        String result = "";
        String[] resultStr;
        String[] resultTempStr;
        int index = 1;
        // 解析数据的可能情况
        // 情况一："PM10" : {"CONCENTRATION" : "17","STATE" : "N","IAQI" : "17","SUBINDEX" : "0.24"} 都有可能有；
        // 情况二："PM10" : {"CONCENTRATION" : "17","STATE" : "N"} 都有可能有；
        // 情况三："PM10_24" : {"CONCENTRATION" : "17","STATE" : "N"} 小时数据会有，其他数据没有；
        for (RegionAirQualityPO po : airDaiaMapList) {
            airEntity = new AIREntity();
            airEntity.setIndex(index++);
            airEntity.setCode(po.getREGIONCODE());
            airEntity.setName(po.getREGIONNAME());
            airEntity.setMonitorTime(po.getMONITORTIME());
            airEntity.setSiteTypeCode(po.getSITETYPECODE());

            airEntity.setPm10_concentration(po.getPM10().getCONCENTRATION());
            airEntity.setPm10_iaqi(po.getPM10().getIAQI() == null ? "" : po.getPM10().getIAQI());
            airEntity.setPm10_subIndex(po.getPM10().getSUBINDEX() == null ? "" : po.getPM10().getSUBINDEX());

            airEntity.setPm25_concentration(po.getPM25().getCONCENTRATION());
            airEntity.setPm25_iaqi(po.getPM25().getIAQI() == null ? "" : po.getPM25().getIAQI());
            airEntity.setPm25_subIndex(po.getPM25().getSUBINDEX() == null ? "" : po.getPM25().getSUBINDEX());

            airEntity.setSo2_concentration(po.getSO2().getCONCENTRATION());
            airEntity.setSo2_iaqi(po.getSO2().getIAQI() == null ? "" : po.getSO2().getIAQI());
            airEntity.setSo2_subIndex(po.getSO2().getSUBINDEX() == null ? "" : po.getSO2().getSUBINDEX());

            airEntity.setNo2_concentration(po.getNO2().getCONCENTRATION());
            airEntity.setNo2_iaqi(po.getNO2().getIAQI() == null ? "" : po.getNO2().getIAQI());
            airEntity.setNo2_subIndex(po.getNO2().getSUBINDEX() == null ? "" : po.getNO2().getSUBINDEX());

            airEntity.setCo_concentration(po.getCO().getCONCENTRATION());
            airEntity.setCo_iaqi(po.getCO().getIAQI() == null ? "" : po.getCO().getIAQI());
            airEntity.setCo_subIndex(po.getCO().getSUBINDEX() == null ? "" : po.getCO().getSUBINDEX());

            airEntity.setO3_concentration(po.getO3().getCONCENTRATION());
            airEntity.setO3_iaqi(po.getO3().getIAQI() == null ? "" : po.getO3().getIAQI());
            airEntity.setO3_subIndex(po.getO3().getSUBINDEX() == null ? "" : po.getO3().getSUBINDEX());

            airEntity.setPm10_24_concentration(po.getPM10_24() == null ? "" : po.getPM10_24().getCONCENTRATION());
            airEntity.setPm25_24_concentration(po.getPM25_24() == null ? "" : po.getPM25_24().getCONCENTRATION());
            airEntity.setO3_8_concentration(po.getO3_8() == null ? "" : po.getO3_8().getCONCENTRATION());

            airEntity.setAqi(po.getAQI());
            airEntity.setAqiLevel(po.getAQILEVEL());
            airEntity.setAqiLevelType(po.getAQILEVELTYPE());
            airEntity.setCompositeValue(po.getCOMPOSITEVALUE());
            airEntity.setPollutant(po.getPOLLUTANT());
            airEntity.setCreateTime(po.getCREATETIME());
            airNewList.add(airEntity);
        }
        return airNewList;
    }

    /**
     * 根据站点编码、时间和表名得到空气质量数据
     *
     * @param paraMap
     * @return
     * @throws Exception
     */

    @Override
    public List<AIREntity> findAirDataBySiteCodesAndTime(Map<String, Object> paraMap) throws Exception {
        // 选中的点位编码
        String checkedSiteCodes = paraMap.get("checkedSiteCodes").toString();
        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // 表名
        String tableName = paraMap.get("tableName").toString();

        // 选择点位编码字符串转换为数组
        String[] checkedSiteCodeArr = checkedSiteCodes.split(",");
        // 选中点位编码数组转换为列表
        List<String> checkedSiteCodeList = Arrays.asList(checkedSiteCodeArr);

        AirParam airParam = new AirParam();
        airParam.setStartTime(startTime);
        airParam.setEndTime(endTime);
        airParam.setSiteCodes(checkedSiteCodeList);
        airParam.setTable(tableName);

        List<SiteAirQualityPO> airDaiaMapList = airDao.findAirDataBySiteCodesAndTime(airParam);
        List<AIREntity> airNewList = new ArrayList<AIREntity>();
        AIREntity airEntity = null;
        String[] resultStr = null;
        String[] resultTempStr = null;
        int index = 1;
        // 解析数据的可能情况
        // 情况一："PM10" : {"CONCENTRATION" : "17","STATE" : "N","IAQI" : "17","SUBINDEX" : "0.24"} 都有可能有；
        // 情况二："PM10" : {"CONCENTRATION" : "17","STATE" : "N"} 都有可能有；
        // 情况三："PM10_24" : {"CONCENTRATION" : "17","STATE" : "N"} 小时数据会有，其他数据没有；
        for (SiteAirQualityPO po : airDaiaMapList) {
            airEntity = new AIREntity();
            airEntity.setIndex(index++);
            airEntity.setCode(po.getSITECODE());
            airEntity.setName(po.getSITENAME());
            airEntity.setMonitorTime(po.getMONITORTIME());
            airEntity.setSiteTypeCode(po.getSITETYPECODE());

            airEntity.setPm10_concentration(po.getPM10().getCONCENTRATION());
            airEntity.setPm10_iaqi(po.getPM10().getIAQI() == null ? "" : po.getPM10().getIAQI());
            airEntity.setPm10_subIndex(po.getPM10().getSUBINDEX() == null ? "" : po.getPM10().getSUBINDEX());

            airEntity.setPm25_concentration(po.getPM25().getCONCENTRATION());
            airEntity.setPm25_iaqi(po.getPM25().getIAQI() == null ? "" : po.getPM25().getIAQI());
            airEntity.setPm25_subIndex(po.getPM25().getSUBINDEX() == null ? "" : po.getPM25().getSUBINDEX());

            airEntity.setSo2_concentration(po.getSO2().getCONCENTRATION());
            airEntity.setSo2_iaqi(po.getSO2().getIAQI() == null ? "" : po.getSO2().getIAQI());
            airEntity.setSo2_subIndex(po.getSO2().getSUBINDEX() == null ? "" : po.getSO2().getSUBINDEX());

            airEntity.setNo2_concentration(po.getNO2().getCONCENTRATION());
            airEntity.setNo2_iaqi(po.getNO2().getIAQI() == null ? "" : po.getNO2().getIAQI());
            airEntity.setNo2_subIndex(po.getNO2().getSUBINDEX() == null ? "" : po.getNO2().getSUBINDEX());

            airEntity.setCo_concentration(po.getCO().getCONCENTRATION());
            airEntity.setCo_iaqi(po.getCO().getIAQI() == null ? "" : po.getCO().getIAQI());
            airEntity.setCo_subIndex(po.getCO().getSUBINDEX() == null ? "" : po.getCO().getSUBINDEX());

            airEntity.setO3_concentration(po.getO3().getCONCENTRATION());
            airEntity.setO3_iaqi(po.getO3().getIAQI() == null ? "" : po.getO3().getIAQI());
            airEntity.setO3_subIndex(po.getO3().getSUBINDEX() == null ? "" : po.getO3().getSUBINDEX());

            airEntity.setPm10_24_concentration(po.getPM10_24() == null ? "" : po.getPM10_24().getCONCENTRATION());
            airEntity.setPm25_24_concentration(po.getPM25_24() == null ? "" : po.getPM25_24().getCONCENTRATION());
            airEntity.setO3_8_concentration(po.getO3_8() == null ? "" : po.getO3_8().getCONCENTRATION());

            airEntity.setAqi(po.getAQI());
            airEntity.setAqiLevel(po.getAQILEVEL());
            airEntity.setAqiLevelType(po.getAQILEVELTYPE());
            airEntity.setCompositeValue(po.getCOMPOSITEVALUE());
            airEntity.setPollutant(po.getPOLLUTANT());
            airEntity.setCreateTime(po.getCREATETIME());
            airNewList.add(airEntity);
        }
        return airNewList;
    }

    @Override
    public MsgResult findAirQualityRegionDataContrastThrend(Map<String, Object> paraMap) throws Exception {
        // 选中的区域编码
        String checkedRegionCodes = paraMap.get("checkedRegionCodes").toString();
        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // 表名
        String tableName = paraMap.get("tableName").toString();
        // 空气质量因子:AQI,综合指数 compositeValue,PM10,PM25,SO2,NO2,CO,O3
        String qualityFactor = paraMap.get("qualityFactor").toString();

        // 选择的区域编码字符串转换为数组
        String[] checkedRegionCodeArr = checkedRegionCodes.split(",");
        // 选中区域编码数组转换为列表
        List<String> checkedRegionCodeList = Arrays.asList(checkedRegionCodeArr);

        // 选择点位 的点位信息
        List<Map<String, Object>> checkedRegionInfoList = regionSiteDao.findRegionInfoByRegionCode(checkedRegionCodeList);
        if (null == checkedRegionInfoList || checkedRegionInfoList.size() == 0) {
            return MsgResult.errorResult("没有查询到选中区域的信息，查询失败。");
        }

        AirParam airParam = new AirParam();
        airParam.setStartTime(startTime);
        airParam.setEndTime(endTime);
        airParam.setRegionCodes(checkedRegionCodeList);
        airParam.setTable(tableName);

        List<RegionAirQualityPO> airDaiaMapList = airDao.findAirDataByRegionCodesAndTime(airParam);
        // 组装eCharts数据
        List<String> siteNameList = new ArrayList<String>();
        List<List<String>> timeList = new ArrayList<List<String>>();
        List<String> timeDetailList = null;
        // 根据tableName，确定返回的timeList，和截取motorTime的长度
        int timeLen = 16;
        if (tableName.contains("MIN")) {
            timeDetailList = this.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), startTime.substring(0, 14) + "00");
        } else if (tableName.contains("HOUR")) {
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 14) + "00", endTime.substring(0, 14) + "00", "hour");
        } else {
            timeLen = 10;
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
        }

        List<List<String>> dataList = new ArrayList<List<String>>();
        List<String> regionDateList = new ArrayList<>();
        boolean monitorExist = false;
        // 截取字段用的
        String[] resultStr = null;
        String[] resultTempStr = null;

        for (Map<String, Object> checkedSiteMap : checkedRegionInfoList) {
            String checkedRegionCode = String.valueOf(checkedSiteMap.get("regionCode"));
            String checkedRegionName = String.valueOf(checkedSiteMap.get("regionName"));
            // 提取点位名字
            siteNameList.add(checkedRegionName);
            regionDateList = new ArrayList<String>();
//			timeDetailList = new ArrayList<String>();
            //遍历时间点数组，如果originalData的MONITORTIME等于时间点，则填充时间；否则填充"-"
            for (String detailTime : timeDetailList) {
                monitorExist = false;
                for (RegionAirQualityPO po : airDaiaMapList) {

                    String regionCode = po.getREGIONCODE();
                    if (regionCode.equals(checkedRegionCode)) {
                        String time = po.getMONITORTIME();
                        if (null == time || "".equals(time)) {
                            continue;
                        }
                        time = time.substring(0, timeLen);
                        // 解析数据的可能情况
                        // 情况一："PM10" : {"CONCENTRATION" : "17","STATE" : "N","IAQI" : "17","SUBINDEX" : "0.24"} 都有可能有；
                        // 情况二："PM10" : {"CONCENTRATION" : "17","STATE" : "N"} 都有可能有；
                        // 情况三："PM10_24" : {"CONCENTRATION" : "17","STATE" : "N"} 小时数据会有，其他数据没有；
                        if (time.equals(detailTime)) {
                            if (qualityFactor.equals("AQI")) {
                                // 提取aqi
                                regionDateList.add(po.getAQI() == null ? "-" : po.getAQI());
                            } else if (qualityFactor.equals("compositeValue")) {
                                // 提取空气质量
                                regionDateList.add(po.getCOMPOSITEVALUE() == null ? "-" : po.getCOMPOSITEVALUE());
                            } else if (qualityFactor.equals("PM10_CONCENTRATION")) {
                                // 提取PM10_CONCENTRATION
                                regionDateList.add(po.getPM10().getCONCENTRATION());
                            } else if (qualityFactor.equals("PM25_CONCENTRATION")) {
                                // 提取PM25_CONCENTRATION
                                regionDateList.add(po.getPM25().getCONCENTRATION());
                            } else if (qualityFactor.equals("SO2_CONCENTRATION")) {
                                // 提取SO2_CONCENTRATION
                                regionDateList.add(po.getSO2().getCONCENTRATION());
                            } else if (qualityFactor.equals("NO2_CONCENTRATION")) {
                                // 提取NO2_CONCENTRATION
                                regionDateList.add(po.getNO2().getCONCENTRATION());
                            } else if (qualityFactor.equals("CO_CONCENTRATION")) {
                                // 提取CO_CONCENTRATION
                                regionDateList.add(po.getCO().getCONCENTRATION());
                            } else if (qualityFactor.equals("O3_CONCENTRATION")) {
                                // 提取O3_CONCENTRATION
                                regionDateList.add(po.getO3().getCONCENTRATION());
                            }
                            monitorExist = true;
                            break;
                        }
                    }
                }
                if (!monitorExist) {
                    regionDateList.add("-");
                }
            }
            timeList.add(timeDetailList);
            dataList.add(regionDateList);

        }  // end for}

        JSONObject resultObj = new JSONObject();
        resultObj.put("siteNameList", siteNameList);
        resultObj.put("timeList", timeList);
        resultObj.put("dataList", dataList);
        return MsgResult.okResult(resultObj);
    }

    @Override
    public MsgResult findAirQualitySiteDataContrastThrend(Map<String, Object> paraMap) {
        // 选中的点位编码
        String checkedSiteCodes = paraMap.get("checkedSiteCodes").toString();
        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // 表名
        String tableName = paraMap.get("tableName").toString();
        // 空气质量因子:AQI,综合指数 compositeValue,PM10,PM25,SO2,NO2,CO,O3
        String qualityFactor = paraMap.get("qualityFactor").toString();

        // 选择点位编码字符串转换为数组
        String[] checkedSiteCodeArr = checkedSiteCodes.split(",");
        // 选中点位编码数组转换为列表
        List<String> checkedSiteCodeList = Arrays.asList(checkedSiteCodeArr);

        // 选择点位 的点位信息
        List<Map<String, Object>> checkedSiteInfoList = regionSiteDao.findSiteInfoBySiteCode(checkedSiteCodeList);
        if (null == checkedSiteInfoList || checkedSiteInfoList.size() == 0) {
            return MsgResult.errorResult("没有查询到选中点位的信息，查询失败。");
        }

        AirParam airParam = new AirParam();
        airParam.setStartTime(startTime);
        airParam.setEndTime(endTime);
        airParam.setSiteCodes(checkedSiteCodeList);
        airParam.setTable(tableName);

        List<SiteAirQualityPO> airDaiaMapList = airDao.findAirDataBySiteCodesAndTime(airParam);

        // 组装eCharts数据
        List<String> siteNameList = new ArrayList<String>();
        List<List<String>> timeList = new ArrayList<List<String>>();
        List<String> timeDetailList = null;
        // 根据tableName，确定返回的timeList，和截取motorTime的长度
        int timeLen = 16;
        if (tableName.contains("MIN")) {
            timeDetailList = this.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), startTime.substring(0, 14) + "00");
        } else if (tableName.contains("HOUR")) {
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 14) + "00", endTime.substring(0, 14) + "00", "hour");
        } else {
            timeLen = 10;
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
        }
        List<List<String>> dataList = new ArrayList<List<String>>();
        List<String> siteDateList = new ArrayList<>();
        boolean monitorExist = false;
        // 截取字段用的
        String[] resultStr = null;
        String[] resultTempStr = null;

        for (Map<String, Object> checkedSiteMap : checkedSiteInfoList) {
            String checkedSiteCode = String.valueOf(checkedSiteMap.get("siteCode"));
            String checkedSiteName = String.valueOf(checkedSiteMap.get("siteName"));
            // 提取点位名字
            siteNameList.add(checkedSiteName);
            siteDateList = new ArrayList<String>();
//			timeDetailList = new ArrayList<String>();
            //遍历时间点数组，如果originalData的MONITORTIME等于时间点，则填充时间；否则填充"-"
            for (String detailTime : timeDetailList) {
                monitorExist = false;
                for (SiteAirQualityPO po : airDaiaMapList) {

                    String siteCode = po.getSITECODE();
                    if (siteCode.equals(checkedSiteCode)) {
                        String time = po.getMONITORTIME();
                        if (null == time || "".equals(time)) {
                            continue;
                        }
                        time = time.substring(0, timeLen);
                        // 解析数据的可能情况
                        // 情况一："PM10" : {"CONCENTRATION" : "17","STATE" : "N","IAQI" : "17","SUBINDEX" : "0.24"} 都有可能有；
                        // 情况二："PM10" : {"CONCENTRATION" : "17","STATE" : "N"} 都有可能有；
                        // 情况三："PM10_24" : {"CONCENTRATION" : "17","STATE" : "N"} 小时数据会有，其他数据没有；
                        if (time.equals(detailTime)) {
                            if (qualityFactor.equals("AQI")) {
                                // 提取aqi
                                siteDateList.add(po.getAQI() == null ? "-" : po.getAQI());
                            } else if (qualityFactor.equals("compositeValue")) {
                                // 提取空气质量
                                siteDateList.add(po.getCOMPOSITEVALUE() == null ? "-" : po.getCOMPOSITEVALUE());
                            } else if (qualityFactor.equals("PM10_CONCENTRATION")) {
                                // 提取PM10_CONCENTRATION
                                siteDateList.add(po.getPM10().getCONCENTRATION());
                            } else if (qualityFactor.equals("PM25_CONCENTRATION")) {
                                // 提取PM25_CONCENTRATION
                                siteDateList.add(po.getPM25().getCONCENTRATION());
                            } else if (qualityFactor.equals("SO2_CONCENTRATION")) {
                                // 提取SO2_CONCENTRATION
                                siteDateList.add(po.getSO2().getCONCENTRATION());
                            } else if (qualityFactor.equals("NO2_CONCENTRATION")) {
                                // 提取NO2_CONCENTRATION
                                siteDateList.add(po.getNO2().getCONCENTRATION());
                            } else if (qualityFactor.equals("CO_CONCENTRATION")) {
                                // 提取CO_CONCENTRATION
                                siteDateList.add(po.getCO().getCONCENTRATION());
                            } else if (qualityFactor.equals("O3_CONCENTRATION")) {
                                // 提取O3_CONCENTRATION
                                siteDateList.add(po.getO3().getCONCENTRATION());
                            }
                            monitorExist = true;
                            break;
                        }
                    }
                }
                if (!monitorExist) {
                    siteDateList.add("-");
                }
            }
            timeList.add(timeDetailList);
            dataList.add(siteDateList);

        }  // end for}

        JSONObject resultObj = new JSONObject();
        resultObj.put("siteNameList", siteNameList);
        resultObj.put("timeList", timeList);
        resultObj.put("dataList", dataList);
        return MsgResult.okResult(resultObj);
    }

    @Override
    public MsgResult findAirQualitySingleRegionDataContrastThrend(Map<String, Object> paraMap) throws Exception {
        // 选中的区域编码
        String checkedRegionCodes = paraMap.get("checkedRegionCodes").toString();
        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // 表名
        String tableName = paraMap.get("tableName").toString();
        // 空气质量因子:AQI,综合指数 compositeValue,PM10,PM25,SO2,NO2,CO,O3
        String qualityFactor = paraMap.get("qualityFactor").toString();

        // 选择的区域编码字符串转换为数组
        String[] checkedRegionCodeArr = checkedRegionCodes.split(",");
        // 选中区域编码数组转换为列表
        List<String> checkedRegionCodeList = Arrays.asList(checkedRegionCodeArr);

        // 选择点位 的点位信息
        List<Map<String, Object>> checkedRegionInfoList = regionSiteDao.findRegionInfoByRegionCode(checkedRegionCodeList);
        if (null == checkedRegionInfoList || checkedRegionInfoList.size() == 0) {
            return MsgResult.errorResult("没有查询到选中区域的信息，查询失败。");
        }

        AirParam airParam = new AirParam();
        airParam.setStartTime(startTime);
        airParam.setEndTime(endTime);
        airParam.setRegionCodes(checkedRegionCodeList);
        airParam.setTable(tableName);

        List<RegionAirQualityPO> airDaiaMapList = airDao.findAirDataByRegionCodesAndTime(airParam);
        // 数据封装
        List<String> siteNameList = new ArrayList<String>();
        siteNameList.add("AQI");
        siteNameList.add("compositeValue");
        siteNameList.add("PM10_CONCENTRATION");
        siteNameList.add("PM25_CONCENTRATION");
        siteNameList.add("SO2_CONCENTRATION");
        siteNameList.add("NO2_CONCENTRATION");
        siteNameList.add("CO_CONCENTRATION");
        siteNameList.add("O3_CONCENTRATION");


        List<List<String>> timeList = new ArrayList<List<String>>();
        List<List<String>> dataList = new ArrayList<List<String>>();
        List<String> timeDetailList = null;
        // 根据tableName，确定返回的timeList，和截取motorTime的长度
        int timeLen = 16;
        if (tableName.contains("MIN")) {
            timeDetailList = this.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), startTime.substring(0, 14) + "00");
        } else if (tableName.contains("HOUR")) {
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 14) + "00", endTime.substring(0, 14) + "00", "hour");
        } else {
            timeLen = 10;
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
        }

        List<String> aqiList = new ArrayList<>();
        List<String> compositeValueList = new ArrayList<>();
        List<String> pm10ConcenrationList = new ArrayList<>();
        List<String> pm25ConcenrationList = new ArrayList<>();
        List<String> so2ConcenrationList = new ArrayList<>();
        List<String> no2ConcenrationList = new ArrayList<>();
        List<String> coConcenrationList = new ArrayList<>();
        List<String> o3ConcenrationList = new ArrayList<>();

        boolean monitorExist = false;
        // 截取字段用的
        String[] resultStr = null;
        String[] resultTempStr = null;

//			timeDetailList = new ArrayList<String>();
        //遍历时间点数组，如果originalData的MONITORTIME等于时间点，则填充时间；否则填充"-"
        for (String detailTime : timeDetailList) {
            monitorExist = false;
            for (RegionAirQualityPO po : airDaiaMapList) {

                String time = po.getMONITORTIME();
                if (null == time || "".equals(time)) {
                    continue;
                }
                time = time.substring(0, timeLen);
                // 解析数据的可能情况
                // 情况一："PM10" : {"CONCENTRATION" : "17","STATE" : "N","IAQI" : "17","SUBINDEX" : "0.24"} 都有可能有；
                // 情况二："PM10" : {"CONCENTRATION" : "17","STATE" : "N"} 都有可能有；
                // 情况三："PM10_24" : {"CONCENTRATION" : "17","STATE" : "N"} 小时数据会有，其他数据没有；
                if (time.equals(detailTime)) {
                    // 提取aqi
                    aqiList.add(po.getAQI() == null ? "-" : po.getAQI());

                    compositeValueList.add(po.getCOMPOSITEVALUE() == null ? "-" : po.getCOMPOSITEVALUE());

                    pm10ConcenrationList.add(po.getPM10().getCONCENTRATION());

                    pm25ConcenrationList.add(po.getPM25().getCONCENTRATION());

                    no2ConcenrationList.add(po.getNO2().getCONCENTRATION());

                    so2ConcenrationList.add(po.getSO2().getCONCENTRATION());

                    coConcenrationList.add(po.getCO().getCONCENTRATION());

                    o3ConcenrationList.add(po.getO3().getCONCENTRATION());

                    monitorExist = true;
                    break;
                }
            }

            if (!monitorExist) {
                aqiList.add("-");
                compositeValueList.add("-");
                pm10ConcenrationList.add("-");
                pm25ConcenrationList.add("-");
                so2ConcenrationList.add("-");
                no2ConcenrationList.add("-");
                coConcenrationList.add("-");
                o3ConcenrationList.add("-");
            }
            timeList.add(timeDetailList);
        }
        dataList.add(aqiList);
        dataList.add(compositeValueList);
        dataList.add(pm10ConcenrationList);
        dataList.add(pm25ConcenrationList);
        dataList.add(so2ConcenrationList);
        dataList.add(no2ConcenrationList);
        dataList.add(coConcenrationList);
        dataList.add(o3ConcenrationList);


        JSONObject resultObj = new JSONObject();
        resultObj.put("siteNameList", siteNameList);
        resultObj.put("timeList", timeList);
        resultObj.put("dataList", dataList);
        return MsgResult.okResult(resultObj);
    }

    @Override
    public MsgResult findAirQualitySingleSiteDataContrastThrend(Map<String, Object> paraMap) {
        // 选中的点位编码
        String checkedSiteCodes = paraMap.get("checkedSiteCodes").toString();
        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // 表名
        String tableName = paraMap.get("tableName").toString();
        // 空气质量因子:AQI,综合指数 compositeValue,PM10,PM25,SO2,NO2,CO,O3
        String qualityFactor = paraMap.get("qualityFactor").toString();

        // 选择点位编码字符串转换为数组
        String[] checkedSiteCodeArr = checkedSiteCodes.split(",");
        // 选中点位编码数组转换为列表
        List<String> checkedSiteCodeList = Arrays.asList(checkedSiteCodeArr);

        // 选择点位 的点位信息
        List<Map<String, Object>> checkedSiteInfoList = regionSiteDao.findSiteInfoBySiteCode(checkedSiteCodeList);
        if (null == checkedSiteInfoList || checkedSiteInfoList.size() == 0) {
            return MsgResult.errorResult("没有查询到选中点位的信息，查询失败。");
        }

        AirParam airParam = new AirParam();
        airParam.setStartTime(startTime);
        airParam.setEndTime(endTime);
        airParam.setSiteCodes(checkedSiteCodeList);
        airParam.setTable(tableName);

        List<SiteAirQualityPO> airDaiaMapList = airDao.findAirDataBySiteCodesAndTime(airParam);
        // 数据封装
        List<String> siteNameList = new ArrayList<String>();
        siteNameList.add("AQI");
        siteNameList.add("compositeValue");
        siteNameList.add("PM10_CONCENTRATION");
        siteNameList.add("PM25_CONCENTRATION");
        siteNameList.add("SO2_CONCENTRATION");
        siteNameList.add("NO2_CONCENTRATION");
        siteNameList.add("CO_CONCENTRATION");
        siteNameList.add("O3_CONCENTRATION");


        List<List<String>> timeList = new ArrayList<List<String>>();
        List<List<String>> dataList = new ArrayList<List<String>>();
        List<String> timeDetailList = null;
        // 根据tableName，确定返回的timeList，和截取motorTime的长度
        int timeLen = 16;
        if (tableName.contains("MIN")) {
            timeDetailList = this.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), startTime.substring(0, 14) + "00");
        } else if (tableName.contains("HOUR")) {
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 14) + "00", endTime.substring(0, 14) + "00", "hour");
        } else {
            timeLen = 10;
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
        }

        List<String> aqiList = new ArrayList<>();
        List<String> compositeValueList = new ArrayList<>();
        List<String> pm10ConcenrationList = new ArrayList<>();
        List<String> pm25ConcenrationList = new ArrayList<>();
        List<String> so2ConcenrationList = new ArrayList<>();
        List<String> no2ConcenrationList = new ArrayList<>();
        List<String> coConcenrationList = new ArrayList<>();
        List<String> o3ConcenrationList = new ArrayList<>();

        boolean monitorExist = false;
        // 截取字段用的
        String[] resultStr = null;
        String[] resultTempStr = null;

//			timeDetailList = new ArrayList<String>();
        //遍历时间点数组，如果originalData的MONITORTIME等于时间点，则填充时间；否则填充"-"
        for (String detailTime : timeDetailList) {
            monitorExist = false;
            for (SiteAirQualityPO po : airDaiaMapList) {

                String time = po.getMONITORTIME();
                if (null == time || "".equals(time)) {
                    continue;
                }
                time = time.substring(0, timeLen);
                // 解析数据的可能情况
                // 情况一："PM10" : {"CONCENTRATION" : "17","STATE" : "N","IAQI" : "17","SUBINDEX" : "0.24"} 都有可能有；
                // 情况二："PM10" : {"CONCENTRATION" : "17","STATE" : "N"} 都有可能有；
                // 情况三："PM10_24" : {"CONCENTRATION" : "17","STATE" : "N"} 小时数据会有，其他数据没有；
                if (time.equals(detailTime)) {

                    aqiList.add(po.getAQI() == null ? "-" : po.getAQI());

                    compositeValueList.add(po.getCOMPOSITEVALUE() == null ? "-" : po.getCOMPOSITEVALUE());

                    pm10ConcenrationList.add(po.getPM10().getCONCENTRATION());

                    pm25ConcenrationList.add(po.getPM25().getCONCENTRATION());

                    no2ConcenrationList.add(po.getNO2().getCONCENTRATION());

                    so2ConcenrationList.add(po.getSO2().getCONCENTRATION());

                    coConcenrationList.add(po.getCO().getCONCENTRATION());

                    o3ConcenrationList.add(po.getO3().getCONCENTRATION());

                    monitorExist = true;
                    break;
                }
            }

            if (!monitorExist) {
                aqiList.add("-");
                compositeValueList.add("-");
                pm10ConcenrationList.add("-");
                pm25ConcenrationList.add("-");
                so2ConcenrationList.add("-");
                no2ConcenrationList.add("-");
                coConcenrationList.add("-");
                o3ConcenrationList.add("-");
            }
            timeList.add(timeDetailList);
        }
        dataList.add(aqiList);
        dataList.add(compositeValueList);
        dataList.add(pm10ConcenrationList);
        dataList.add(pm25ConcenrationList);
        dataList.add(so2ConcenrationList);
        dataList.add(no2ConcenrationList);
        dataList.add(coConcenrationList);
        dataList.add(o3ConcenrationList);


        JSONObject resultObj = new JSONObject();
        resultObj.put("siteNameList", siteNameList);
        resultObj.put("timeList", timeList);
        resultObj.put("dataList", dataList);
        return MsgResult.okResult(resultObj);
    }

    @Override
    public List<Map> findAirDataByTime(Map<String, Object> paraMap) throws Exception {
        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // 表名
        String tableName = paraMap.get("tableName").toString();
        // 设备编码
        String queryCheckCode = paraMap.get("queryCheckCode").toString();
        // 选择的设备编码字符串转换为数组
        String[] queryCheckCodeArr = queryCheckCode.split(",");
        // 选中设备编码数组转换为列表
        List<String> checkedCodeList = Arrays.asList(queryCheckCodeArr);

        AirParam airParam = new AirParam();
        airParam.setStartTime(startTime);
        airParam.setEndTime(endTime);
        airParam.setTable(tableName);
        airParam.setSiteCodes(checkedCodeList);

        List<MonitorBaseDataPo> airDaiaMapList = airDao.findAirDataByTime(airParam);
        List<Map> mapList = new ArrayList<>();
        Map<String, Object> map = null;
        MonitorDataPo dataPo;
        int index = 1;
        for (MonitorBaseDataPo po : airDaiaMapList) {
            map = new HashMap<>();
            map.put("index", index++);
            map.put("snCode", po.getSNCODE());
            map.put("monitorCode", po.getMONITORCODE());
            map.put("monitorTime", po.getMONITORTIME());
            map.put("calculFlag", po.getCALCULFLAG());
            map.put("createTime", po.getCREATETIME());
            map.put("dataSource", po.getDATASOURCE());
            map.put("t1d", po.getT1D());
            map.put("t1h", po.getT1H());
            map.put("t5m", po.getT5M());

            dataPo = po.getMONITORDATA();
            map.put("pm25", String.valueOf(dataPo.getPM25()));
            map.put("pm10", String.valueOf(dataPo.getPM10()));
            map.put("co", String.valueOf(dataPo.getCO()));
            map.put("so2", String.valueOf(dataPo.getSO2()));
            map.put("no2", String.valueOf(dataPo.getNO2()));
            map.put("o3", String.valueOf(dataPo.getO3()));

            map.put("ctpm10", String.valueOf(dataPo.getCTPM10()));
            map.put("ctpm25", String.valueOf(dataPo.getCTPM25()));
            map.put("ctco", String.valueOf(dataPo.getCTCO()));
            map.put("ctso2", String.valueOf(dataPo.getCTSO2()));
            map.put("ctno2", String.valueOf(dataPo.getCTNO2()));
            map.put("cto3", String.valueOf(dataPo.getCTO3()));

            map.put("adco", String.valueOf(dataPo.getADCO()));
            map.put("adso2", String.valueOf(dataPo.getADSO2()));
            map.put("adno2", String.valueOf(dataPo.getADNO2()));
            map.put("ado3", String.valueOf(dataPo.getADO3()));

            map.put("hum", String.valueOf(dataPo.getHUM() == null ? "0.0" : dataPo.getHUM()));
            map.put("temp", String.valueOf(dataPo.getTEMP() == null ? "0.0" : dataPo.getTEMP()));
            map.put("wd", WeatherUtil.changeAngleToWindDirection(String.valueOf(dataPo.getWD())));
            map.put("ws", WeatherUtil.changeWindSpeedToPower(String.valueOf(dataPo.getWS())));

            mapList.add(map);
        }

        return mapList;
    }


    private List<String> getDatesBetweenTwoDate(String startTime, String endTime, String calenderStartTime) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        int time_increment = Calendar.MINUTE;
        List<String> dateList = new ArrayList<>();
        dateList.add(startTime);

        try {
            calendar.setTime(sdf.parse(calenderStartTime));
            while (true) {
                //根据日历的规则，为给定的日历字段添加或减去指定的时间量
                calendar.add(time_increment, 5);
                // 测试此日期是否在指定日期之后
                if (sdf.parse(endTime).after(calendar.getTime())) {
                    if (sdf.parse(startTime).before(calendar.getTime())) {

                        dateList.add(sdf.format(calendar.getTime()));
                    }
                } else {
                    break;
                }

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        dateList.add(endTime);
        return dateList;
    }
}
